﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    class TeacherPlanDAO : CommonDAO
    {
        private const string sql_get_teacher_student_count = "select CurNum From TeacherPlan where TeacherID = @TeacherID";
        private const string sql_inc_teacher_student_count = "update TeacherPlan set CurNum = @CurNum where TeacherID = @TeacherID";
        private const string sql_get_teacher_student_plan_count = "select CurNum,PlanNum From TeacherPlan where TeacherID = @TeacherID";
       
        public void addStudent(string teacherId)
        {
            SqlCommand cmd = new SqlCommand(sql_get_teacher_student_count);
            cmd.Parameters.Add("@TeacherID", SqlDbType.VarChar, 10).Value = teacherId;
            DataSet ds = this.executeCommand(cmd, "TeacherPlan");
            int curNum = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            // add a student
            ++curNum;
            cmd = new SqlCommand(sql_inc_teacher_student_count);
            cmd.Parameters.Add("@TeacherID", SqlDbType.VarChar, 10).Value = teacherId;
            cmd.Parameters.Add("@CurNum", SqlDbType.Int, 4).Value = curNum;
            this.ExecuteNonQueryCommand(cmd);
            return;
        }

        public bool isTeacherFull(string teacherId)
        {
            SqlCommand cmd = new SqlCommand(sql_get_teacher_student_plan_count);
            cmd.Parameters.Add("@TeacherID", SqlDbType.VarChar, 10).Value = teacherId;
            DataSet ds = this.executeCommand(cmd, "TeacherPlan");
            int curNum = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            int planNum = Convert.ToInt32(ds.Tables[0].Rows[0][1]);
            if (curNum == planNum)
            {
                return true;
            }
            return false;
        }

        public void eraseStudent(string strTeacherID)
        {
            SqlCommand cmd = new SqlCommand(sql_get_teacher_student_count);
            cmd.Parameters.Add("@TeacherID", SqlDbType.VarChar, 10).Value = strTeacherID;
            DataSet ds = this.executeCommand(cmd, "TeacherPlan");
            int curNum = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            // add a student
            -- curNum;
            cmd = new SqlCommand(sql_inc_teacher_student_count);
            cmd.Parameters.Add("@TeacherID", SqlDbType.VarChar, 10).Value = strTeacherID;
            cmd.Parameters.Add("@CurNum", SqlDbType.Int, 4).Value = curNum;
            this.ExecuteNonQueryCommand(cmd);
            return;
        }
    }
}
